package cn.getech.data.development.controller;

import cn.getech.data.development.entity.ranger.RangerGroup;
import cn.getech.data.development.model.ranger.RangerUser;
import cn.getech.data.development.service.RangerDataService;
import cn.getech.data.development.service.UserRangerPolicyRelService;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.getech.system.center.model.req.role.RoleUsers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Api(tags = "数据权限-ranger")
@RestController
@RequestMapping("/bdp/ranger")
public class RangerController {

    @Autowired
    private RangerDataService rangerDataService;
    @Autowired
    private UserRangerPolicyRelService userRangerPolicyRelService;

    @ApiOperation("新增ranger用户")
    @PostMapping("/addRangerUser")
    @SysLog(value = "新增ranger用户", type = OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R addRangerUser(@RequestBody RangerUser rangerUser) {
        RangerUser rangerUser1 = rangerDataService.addRangerUser(rangerUser);
        return R.okWithData(rangerUser1);
    }

    @ApiOperation("查询ranger用户")
    @GetMapping("/getRangerUser")
    @SysLog(value = "查询ranger用户", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R getRangerUser(@RequestParam(value = "userName") String userName) {
        RangerUser rangerUser = rangerDataService.getRangerUser(userName);
        return R.okWithData(rangerUser);
    }

    @ApiOperation("删除ranger用户")
    @PostMapping("/deleteRangerUser/{userName}")
    @SysLog(value = "删除ranger用户", type = OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R deleteRangerUser(@PathVariable(value = "userName") String userName) {
        rangerDataService.deleteRangerUser(userName);
        //删除这个用户对应的策略
        rangerDataService.deleteRangerByRangerUserName(userName);
        return R.ok();
    }

    @ApiOperation("修改超管策略用户")
    @PostMapping("/putRangerPolicy")
    @SysLog(value = "修改超管策略用户", type = OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R putRangerPolicy(@RequestBody List<RoleUsers> roleList) {
        rangerDataService.updateRangerPolicy(roleList);
        return R.ok();
    }

    @ApiOperation("修改某用户ranger的策略")
    @PostMapping("/updateRangerPolicy/{userId}")
    @SysLog(value = "修改某用户ranger的策略", type = OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R updateRangerPolicy(@PathVariable(value = "userId") Long userId) {
        List<Long> userIds = Arrays.asList(userId);
        rangerDataService.genteratorRangerPolicy(userIds);
        return R.ok();
    }

    /**
     * 初始化所有的用户对应的策略到ranger中去
     */
    @ApiOperation("初始化所有的用户对应的策略到ranger中去")
    @GetMapping("/initPolicy")
    @SysLog(value = "初始化所有的用户对应的策略到ranger中去", type = OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R initPolicy() {
        rangerDataService.initPolicyDataList();
        return R.ok();
    }


    /**
     * 删除ranger中所有的策略
     */
    @ApiOperation("初始化所有的用户对应的策略到ranger中去")
    @GetMapping("/delRangerPolicy")
    @SysLog(value = "初始化所有的用户对应的策略到ranger中去", type = OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R delRangerPolicy() {
        List<Integer> exCludAllList = new ArrayList<>();
        exCludAllList.add(1);
        exCludAllList.add(2);
        exCludAllList.add(3);
        exCludAllList.add(4);
        exCludAllList.add(4501);
        for (int i = 5; i < 7000; i++) {
            try {
                rangerDataService.deleteRangerPolicy(i);
            } catch (Exception e) {
                log.error("删除策略异常id:{}，error:{}", i, e.getMessage());
            }
        }
        try {
            rangerDataService.deleteAllUserRangerRel();
        } catch (Exception e) {
            log.error("删除数据库管理策略的表数据异常：{}", e.getMessage());
        }
        return R.ok();
    }


    @ApiOperation("新增ranger用户组")
    @PostMapping("/addRangerGroup")
    @SysLog(value = "新增ranger用户组", type = OperationTypeEnum.ADDCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R addRangerGroup(@RequestBody RangerGroup rangerGroup) {
        RangerGroup rangerGroup1 = rangerDataService.addRangerGroup(rangerGroup);
        return R.okWithData(rangerGroup1);
    }

    @ApiOperation("删除ranger用户组")
    @PostMapping("/deleteRangerGroup/{groupName}")
    @SysLog(value = "删除ranger用户组", type = OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R  deleteRangerGroup(@PathVariable(value = "groupName") String groupName) {
        rangerDataService.deleteRangerGroup(groupName);
        return R.ok();
    }

    @ApiOperation("查询ranger用户组信息")
    @GetMapping("/getRangerGroup")  ///bdp/ranger/getRangerGroup
    @SysLog(value = "查询ranger用户组信息", type = OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.AUTH_MANAGER_MODULE_CODE)
    public R getRangerGroup(@RequestParam(value = "groupName")String groupName) {
        RangerGroup rangerGroup = rangerDataService.getRangerGroup(groupName);
        return R.okWithData(rangerGroup);
    }

}
